package com.prs.crm.action.stock;

import java.util.Collection;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;

import com.prs.crm.action.FlowAction;
import com.prs.crm.annotation.Clew;
import com.prs.crm.annotation.FlowActive;
import com.prs.crm.annotation.FlowStart;
import com.prs.crm.domain.stock.Product;
import com.prs.crm.domain.stock.ProductStore;
import com.prs.crm.domain.store.StoreFlit;
import com.prs.crm.domain.store.StoreFlitDetail;
import com.prs.crm.domain.store.StoreRoom;
import com.prs.crm.domain.sys.Code;
import com.prs.crm.domain.sys.CodeInvoice;
import com.prs.crm.service.stock.ProductService;
import com.prs.crm.service.stock.ProductStoreService;
import com.prs.crm.service.stock.StoreFlitService;
import com.prs.crm.service.stock.StoreRoomService;
import com.prs.crm.service.sys.CodeInvoiceService;
import com.prs.crm.service.sys.CodeService;

public class StoreFlitAction extends FlowAction<StoreFlit> {

	private static final long serialVersionUID = 1L;
	
	private String Encode;
	
	private CodeInvoice codeInvoice;
	
	private CodeInvoiceService codeInvoiceService;
	
	private Code code;
	
	private CodeService codeService;
	
	private Collection<CodeInvoice> codelist;	

	private StoreFlit storeflit;
	
	private ProductService productService;
	
	private Product[] products;
	
	private StoreRoom[] rooms;
	
	private Integer[]  pronums;
	
	private String[] names;
	
	private Integer[] storeoutNums;
	
	private String[] remarks;
	
	@Autowired
	private StoreFlitService storeflitService;

	@Autowired
	private StoreRoomService storeRoomService;
	
	@Autowired
	private ProductStoreService productStoreService;

	private PaginationSupport pagination;

	private List<StoreRoom> storerooms;
	
	private Collection<StoreFlitDetail> slist;
	
	private List<Product> productlist;
	
	private String batchNumber;
	private String listString;
	
	@FlowActive(target = "storeflit", flowservice = "storeflitService")
	@Action("editStoreFlit")
	public String editStoreFlit() {
		this.codelist = this.getCodeInvoiceService().getInvoiceCodeType("库存调拨");
		this.storerooms = this.storeRoomService.getAllstoreroom();
		return SUCCESS;
	}

	@Clew(results = { "success", "none" })
	@FlowStart(target = "storeflit", flowservice = "storeflitService")
	@Action(value = "saveStoreFlit", results = {
			@Result(name = "success", type = "redirect", location = "listStoreFlit"),
			@Result(name = "error", type = "chain", location = "editStoreFlit"),
			@Result(name = "none", type = "redirect", location = "listStoreFlit") })
	public String saveStoreFlit() {

		if ("n".equals(Encode)) {
				int EncodeRplice = this.getCodeService().getBackRpliceAll("手动编码","库房调拨",
						storeflit.getStoreFlitCode());
				if (EncodeRplice != 0) {
					this.addActionError("编码重复!请重新输入");
					return ERROR;
				}
			}
		if("x".equals(Encode)||"".equals(Encode)){
			this.addActionError("请选择编码类型");		
			return ERROR;
		}
		if(storeflit.getFlitOutRoom()==storeflit.getFlitInRoom()){
			this.addActionError("仓库不能一样");
			return ERROR;
		}
		if(storeflit.getFlitName()==null||storeflit.getFlitName().equals("")){
			this.addActionError("主题不能为空");
			return ERROR;
		}
		if(storeflit.getFlitOutRoom()==null){
			this.addActionError("调出仓库不能太为空");
			return ERROR;
		}if(storeflit.getFlitInRoom()==null){
			this.addActionError("调进仓库不能太为空");
			return ERROR;
		}
		if(storeflit.getBatchNum()==null||storeflit.getBatchNum().equals("")){
			this.addActionError("批号不能太为空");
			return ERROR;
		}
		if(products==null){
			this.addActionError("明细不能太为空");
			return ERROR;
		}
		/**
		 * 编码产生
		 */
		if ("n".equals(Encode)) {
			Code codes = new Code();
			codes.setCodeValue(storeflit.getStoreFlitCode());
            codes.setCodeMolde("库房调拨");
            codes.setEntityName(this.getClass().getName());
			codes.setCodeType("手动编码");
			this.getCodeService().save(codes);
			storeflit.setCode(codes);
			this.getStoreflitService().saveAllStoreFlit(storeflit, products, pronums, names, storeoutNums, remarks, rooms);
		} else {
			CodeInvoice Vob = this.getCodeInvoiceService().getVobject(
					codeInvoice.getId());
			Code codes = new Code();
			codes.setCodeMolde("库房调拨");
	        codes.setEntityName(this.getClass().getName());
			codes.setCodeValue(storeflit.getStoreFlitCode());
			Vob.setNumBackUp(Increase(Vob.getNumBackUp()));
			this.getCodeInvoiceService().saveOrUpdate(Vob);
			codes.setCodeType(Vob.getCodePlan());
			this.getCodeService().save(codes);
			storeflit.setCode(codes);
			this.getStoreflitService().saveAllStoreFlit(storeflit, products, pronums, names, storeoutNums, remarks, rooms);
		
		}
		
		this.setPrompt("操作成功");
		return SUCCESS;
	}
	
	@Action("listStoreFlit")
	public String excute() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(StoreFlit.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getStoreflitService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	
	@Action("viewStoreFlit")
	public String viewStoreFlit() {
		storeflit = this.getStoreflitService().get(storeflit.getId());
		this.slist = storeflit.getDetails();
		this.displayEndNodes(storeflit);
		return SUCCESS;
	}
	
	
	@Action("selectProductStoreFlit")
	public String selectProductStoreFlit() {	
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(ProductStore.class);
		criteria.add(Restrictions.eq("room",storeflit.getFlitOutRoom()));	
		if(batchNumber!=null &&  (!batchNumber .equals(""))){
		   criteria.add(Restrictions.eq("batchNum",batchNumber));	
		}
		this.setPagination(this.getProductStoreService().listByPage(criteria,
				getPaginationHelper().prepare(pagination)));	
		return SUCCESS;
	}
	
	
	
	public static String Increase(String backupNum) {
		String Inc = "";
		int lengths = backupNum.length();
		String lengthss = (Integer.parseInt(backupNum) + 1) + "";
		int increase = (lengthss.trim()).length();
		if (lengths != increase) {
			String In = "";
			for (int i = 0; i < lengths - increase; i++) {
				In = In + "0";
			}
			In = (In + lengthss).trim();
			Inc = In;
		} else {
			Inc = lengthss;
		}
		return Inc.trim();
	}
	
	

	public StoreFlit getStoreflit() {
		return storeflit;
	}

	public void setStoreflit(StoreFlit storeflit) {
		this.storeflit = storeflit;
	}

	public StoreFlitService getStoreflitService() {
		return storeflitService;
	}

	public void setStoreflitService(StoreFlitService storeflitService) {
		this.storeflitService = storeflitService;
	}

	public StoreRoomService getStoreRoomService() {
		return storeRoomService;
	}

	public void setStoreRoomService(StoreRoomService storeRoomService) {
		this.storeRoomService = storeRoomService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public List<StoreRoom> getStorerooms() {
		return storerooms;
	}

	public void setStorerooms(List<StoreRoom> storerooms) {
		this.storerooms = storerooms;
	}

	public Collection<StoreFlitDetail> getSlist() {
		return slist;
	}

	public void setSlist(Collection<StoreFlitDetail> slist) {
		this.slist = slist;
	}

	public ProductStoreService getProductStoreService() {
		return productStoreService;
	}

	public void setProductStoreService(ProductStoreService productStoreService) {
		this.productStoreService = productStoreService;
	}

	public Product[] getProducts() {
		return products;
	}

	public void setProducts(Product[] products) {
		this.products = products;
	}

	public Integer[] getPronums() {
		return pronums;
	}

	public void setPronums(Integer[] pronums) {
		this.pronums = pronums;
	}

	public String[] getNames() {
		return names;
	}

	public void setNames(String[] names) {
		this.names = names;
	}

	public Integer[] getStoreoutNums() {
		return storeoutNums;
	}

	public void setStoreoutNums(Integer[] storeoutNums) {
		this.storeoutNums = storeoutNums;
	}

	public String[] getRemarks() {
		return remarks;
	}

	public void setRemarks(String[] remarks) {
		this.remarks = remarks;
	}

	public StoreRoom[] getRooms() {
		return rooms;
	}

	public void setRooms(StoreRoom[] rooms) {
		this.rooms = rooms;
	}

	public ProductService getProductService() {
		return productService;
	}

	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	public List<Product> getProductlist() {
		return productlist;
	}

	public void setProductlist(List<Product> productlist) {
		this.productlist = productlist;
	}

	public String getBatchNumber() {
		return batchNumber;
	}

	public void setBatchNumber(String batchNumber) {
		this.batchNumber = batchNumber;
	}

	public String getListString() {
		return listString;
	}

	public void setListString(String listString) {
		this.listString = listString;
	}

	public String getEncode() {
		return Encode;
	}

	public void setEncode(String encode) {
		Encode = encode;
	}

	public CodeInvoice getCodeInvoice() {
		return codeInvoice;
	}

	public void setCodeInvoice(CodeInvoice codeInvoice) {
		this.codeInvoice = codeInvoice;
	}

	public CodeInvoiceService getCodeInvoiceService() {
		return codeInvoiceService;
	}

	public void setCodeInvoiceService(CodeInvoiceService codeInvoiceService) {
		this.codeInvoiceService = codeInvoiceService;
	}

	public Code getCode() {
		return code;
	}

	public void setCode(Code code) {
		this.code = code;
	}

	public CodeService getCodeService() {
		return codeService;
	}

	public void setCodeService(CodeService codeService) {
		this.codeService = codeService;
	}

	public Collection<CodeInvoice> getCodelist() {
		return codelist;
	}

	public void setCodelist(Collection<CodeInvoice> codelist) {
		this.codelist = codelist;
	}
 
	
	
	
}
